from DBUtil import DBUtil
import requests
from bs4 import BeautifulSoup
import time
from threading import Timer
class Spider(object):

    # 封装http请求、获取响应数据
    def __fetch_content(self):
        i=4;
        while i<6:
            page = "p"+str(i)+"/v9/s9/t0/u0/i1/ALL.html"
            url = "http://book.zongheng.com/store/c1/c0/b0/u0/"+page
        #     url="http://book.zongheng.com/store/c1/c0/b0/u0/p1/v9/s9/t0/u0/i1/ALL.html"
            headers = {'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
            response = requests.get(url, headers=headers)
            response.encoding = 'utf-8'
            try:
                r = requests.get(url)
                r.raise_for_status()
                r.encoding = r.apparent_encoding
                self.__analysis(r.text)
            except:
                print("错误")
            r = requests.get(url)
            r.raise_for_status()
            # r.encoding = r.apparent_encoding
            # self.__analysis(r.text)
            # t = Timer(3, Spider)
            # t.start()
            i = i + 1
    # 解析数据
    def __analysis(self, html):
        soup = BeautifulSoup(html, "html.parser")
        book_list = soup.find_all(class_="bookinfo")
        records = []
        for item in book_list:
            b = item.find_all('a')  # 在每个对应div标签下找span标签，会发现，一个a里面有四组span
            novel_name = b[0].string
            author = b[1].string
            book_type= b[2].string
            end_time=item.find(class_="bookupdate").get_text()
            intro=item.find(class_="bookintro").get_text()
            create_by = "沐歌"
            record={'book_type':book_type,
                    'novel_name':novel_name,
                    'author':author,
                    'end_time':end_time,
                    'intro':   intro,
                    'create_by':create_by
                    }
            print(record)
            records.append(record)

        self.__storage(records)


    # 存数据库
    def __storage(self, records):
        db = DBUtil()
        db.batch_insert(records)

    def __show(self):
        pass

    def go(self):
        html = self.__fetch_content()
        # records=self.__analysis(html)
        # self.__storage(records)


if __name__ == "__main__":
    spider = Spider()
    spider.go()
